<template>
  <el-dialog
    :title="formData.id ? '编辑库存记录' : '新增库存记录'"
    :model-value="visible"
    @update:model-value="$emit('update:visible', $event)"
    width="800px"
    :before-close="handleClose">
    
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      label-position="right">
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="物料编码" prop="materCode">
            <el-input
              v-model="formData.materCode"
              placeholder="请输入物料编码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料名称" prop="materName">
            <el-input
              v-model="formData.materName"
              placeholder="请输入物料名称" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="物料分类" prop="materType">
            <el-select v-model="formData.materType" placeholder="请选择物料分类" style="width: 100%" clearable>
              <el-option label="原材料" value="1" />
              <el-option label="半成品" value="2" />
              <el-option label="成品" value="3" />
              <el-option label="辅料" value="4" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="批次号" prop="batchCode">
            <el-input
              v-model="formData.batchCode"
              placeholder="请输入批次号" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="仓库名称" prop="warehouseName">
            <el-input
              v-model="formData.warehouseName"
              placeholder="请输入仓库名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="库位名称" prop="locationName">
            <el-input
              v-model="formData.locationName"
              placeholder="请输入库位名称" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="数量" prop="quantity">
            <el-input
              v-model="formData.quantity"
              placeholder="请输入数量"
              type="number"
              step="0.01"
              :min="0">
              <template #append>
                <span>单位</span>
              </template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类型" prop="type" >
            <el-radio-group v-model="formData.type">
              <el-radio label="入库">入库</el-radio>
              <el-radio label="出库">出库</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="formData.status">
              <el-radio label="10">启用</el-radio>
              <el-radio label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规格" prop="spec">
            <el-input
              v-model="formData.spec"
              placeholder="请输入规格" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="筒仓编码" prop="siloCode">
            <el-input
              v-model="formData.siloCode"
              placeholder="请输入筒仓编码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="筒仓名称" prop="siloName">
            <el-input
              v-model="formData.siloName"
              placeholder="请输入筒仓名称" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="formData.remark"
          type="textarea"
          :rows="3"
          placeholder="请输入备注信息"
          maxlength="500"
          show-word-limit />
      </el-form-item>
    </el-form>
    
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading">
        {{ formData.id ? '更新' : '创建' }}
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, watch, nextTick } from 'vue'
import { ElMessage } from 'element-plus'

// 定义 props
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  formData: {
    type: Object,
    default: () => ({})
  }
})

// 定义 emits
const emit = defineEmits(['update:visible', 'submit', 'close'])

// 响应式数据
const loading = ref(false)
const formRef = ref()

// 表单验证规则
const formRules = {
  materCode: [
    { required: true, message: '请输入物料编码', trigger: 'blur' },
    { min: 1, max: 50, message: '物料编码长度在 1 到 50 个字符', trigger: 'blur' }
  ],
  materName: [
    { required: true, message: '请输入物料名称', trigger: 'blur' },
    { min: 1, max: 100, message: '物料名称长度在 1 到 100 个字符', trigger: 'blur' }
  ],
  materType: [
    { required: true, message: '请选择物料分类', trigger: 'change' }
  ],
  batchCode: [
    { required: true, message: '请输入批次号', trigger: 'blur' }
  ],
  warehouseName: [
    { required: true, message: '请输入仓库名称', trigger: 'blur' }
  ],
  quantity: [
    { required: true, message: '请输入数量', trigger: 'blur' },
    { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入正确的数字格式', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请选择类型', trigger: 'change' }
  ],
  status: [
    { required: true, message: '请选择状态', trigger: 'change' }
  ]
}

// 监听 visible 变化，重置表单验证
watch(() => props.visible, (newVal) => {
  if (newVal) {
    nextTick(() => {
      if (formRef.value) {
        formRef.value.clearValidate()
      }
    })
  }
})

// 关闭弹窗
const handleClose = () => {
  emit('update:visible', false)
  emit('close')
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    // 表单验证
    await formRef.value.validate()
    
    loading.value = true
    
    // 触发提交事件，传递表单数据
    emit('submit', { ...props.formData })
    
  } catch (error) {
    if (error.errors) {
      ElMessage.error('请完善表单信息')
    }
  } finally {
    loading.value = false
  }
}

// 暴露方法给父组件
defineExpose({
  setLoading: (value) => {
    loading.value = value
  }
})
</script>

<style scoped>
/* 弹窗样式优化 */
:deep(.el-dialog__body) {
  padding: 20px;
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-input-number) {
  width: 100%;
}
</style>